#include "sys.h"

//////////////////////////////////////////////////////////////////////////////////	 
//作者：RIU									  
////////////////////////////////////////////////////////////////////////////////// 	 

//void TIM4Delay_ms(uint16_t Times)
//{
//	TIM_Cmd(TIM4,ENABLE);//使能定时器4
//	while(Times--)
//	{
//		while(TIM_GetITStatus(TIM4, TIM_IT_Update) == RESET); //TIM4没进中断即1ms没到
//		TIM_ClearITPendingBit(TIM4, TIM_IT_Update  ); //到这里说明进了中断，到了1ms
//	}
//}


//定时器4中断服务程序		    
void TIM4_IRQHandler(void)
{ 	
	float Filter_Angle=0.85;
	if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)//是更新中断
	{	 			   
		TIM_ClearITPendingBit(TIM4, TIM_IT_Update  );  //清除TIM4更新中断标志   	
		Filter_Angle = LowPassFilter_RemoveExtremumAverage(Leida_Angle,10);
		//u5_printf("filter:%f\r\n",Filter_Angle);
		//turn_flag = 2;
		//u5_printf("L:%.2f, R:%.2f, B:%.2f\r\n", Leida_Distance[0], Leida_Distance[1], Leida_Distance[2]);
		if(carstate!=2&&carstate!=4&&turn_flag==0&&order_flag==0)order_num(3,'D',Target_Leida_A-Filter_Angle);	
		//u5_printf("Target_Leida_A:%f\r\n",Target_Leida_A);
	}	    
}
 
//通用定时器中断初始化
//这里始终选择为APB1的2倍，而APB1为36M
//arr：自动重装值。
//psc：时钟预分频数		 
void TIM4_Int_Init(u16 arr,u16 psc)
{	
	NVIC_InitTypeDef NVIC_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//TIM4时钟使能    
	
	//定时器TIM4初始化
	TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值	
	TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
	TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
 
	TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE ); //使能指定的TIM4中断,允许更新中断
	
	NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;		//子优先级3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ通道使能
	NVIC_Init(&NVIC_InitStructure);	//根据指定的参数初始化VIC寄存器
   
	TIM_Cmd(TIM4,ENABLE);//使能定时器4	
}
	 
